package com.example.pro1.controller.lian;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.example.pro1.entity.lian.*;
import com.example.pro1.entity.lian.pojoMid.GorderWithGoodInfo;
import com.example.pro1.entity.lian.pojoMid.cartWithGoodInfo;
import com.example.pro1.entity.lian.pojoMid.evaluationWithCustomerWithImg;
import com.example.pro1.service.lian.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
@ResponseBody
@RequestMapping("/customer")
@CrossOrigin
public class customerController {

    @Autowired
    CustomerService CustomerService;

    @Autowired
    AddressService AddressService;

    @Autowired
    com.example.pro1.service.lian.CollectionService CollectionService;

    @Autowired
    goodController goodController;

    @Autowired
    com.example.pro1.service.lian.CartService CartService;

    @Autowired
    com.example.pro1.service.lian.GorderService GorderService;

    @Autowired
    GoodpreService GoodpreService;

    @Autowired
    com.example.pro1.service.lian.EvaluationService EvaluationService;

    @Autowired
    com.example.pro1.service.lian.EvaluateimgService EvaluateimgService;

    @Autowired
    ReturngoodService ReturngoodService;

    /*0.根据客户的openId获取cId*/
    @RequestMapping("/getCIdByOpenId")
    public String getCIdByOpenId(@RequestParam("openId") String openId){
        LambdaQueryWrapper<Customer> CustomerQueryWrapper = new LambdaQueryWrapper<>();
        CustomerQueryWrapper.eq(StringUtils.isNotBlank(openId),Customer::getCwxopenid,openId);
        Customer customer = CustomerService.getOne(CustomerQueryWrapper);
        return customer.getCid();
    }

    /*1.根据客户id获取客户信息*/
    @RequestMapping("/getCustomerInfoByCId")
    public Customer getCustomerInfoByCId(@RequestParam("cId")String cId){
        LambdaQueryWrapper<Customer> CustomerQueryWrapper = new LambdaQueryWrapper<>();
        CustomerQueryWrapper.eq(StringUtils.isNotBlank(cId),Customer::getCid,cId);
        Customer customer = CustomerService.getOne(CustomerQueryWrapper);
        return customer;
    }

    /*2.根据客户openId获取客户信息*/
    @RequestMapping("/getCustomerInfoByOpenId")
    public Customer getCustomerInfoByOpenId(@RequestParam("openId") String openId){
        /*首先根据openId获取cid*/
        String cId = getCIdByOpenId(openId);
        /*根据cId获取客户信息*/
        return getCustomerInfoByCId(cId);
    }

    /*3.根据客户id获取地址信息*/
    @RequestMapping("/getAddressByCId")
    public List<Address> getAddressByCId(@RequestParam("cId")String cId){
        /*根据cid获取地址信息*/
        LambdaQueryWrapper<Address> AddressQueryWrapper = new LambdaQueryWrapper<>();
        AddressQueryWrapper.eq(StringUtils.isNotBlank(cId),Address::getCid,cId);
        return AddressService.list(AddressQueryWrapper);
    }

    /*4.根据客户openId获取客户地址信息*/
    @RequestMapping("/getAddressByOpenId")
    public List<Address> getAddressByOpenId(@RequestParam("openId")String openId){
        /*根据openId获取cId*/
        String cId = getCIdByOpenId(openId);
        /*根据客户id获取地址信息*/
        return getAddressByCId(cId);
    }

    /*5.根据客户cId获取对应的收藏gId列表list*/
    @RequestMapping("/getCollectionGIdByCId")
    public List<String> getCollectionGIdByCId(@RequestParam("cId") String cId){
        LambdaQueryWrapper<Collection> CollectionQueryWrapper = new LambdaQueryWrapper<>();
        CollectionQueryWrapper.eq(StringUtils.isNotBlank(cId),Collection::getCid,cId);
        List<String> gIdList = new ArrayList<>();
        for (Collection collection : CollectionService.list(CollectionQueryWrapper)) {
            gIdList.add(collection.getGid());
        }
        return  gIdList;
    }


    /*5.1 根据客户openId获取对应的收藏gId列表list*/
    @RequestMapping("/getCollectionGIdByOpenId")
    public List<String> getCollectionGIdByOpenId(@RequestParam("openId") String openId){
        /*根据openId获取cId*/
        String cId = getCIdByOpenId(openId);
        /*根据cId获取收藏的gId列表*/
        return getCollectionGIdByCId(cId);
    }


    /*6.根据客户的cId获取对应的收藏good对象列表*/
    @RequestMapping("/getCollectionGoodInfoByCId")
    public List<Good> getCollectionGoodInfoByCId(@RequestParam("cId") String cId){
        /*根据cId获取goodList*/
        List<String> gIdList = getCollectionGIdByCId(cId);
        /*根据gId获取商品信息*/
        return goodController.getGoodsInfoByIds(gIdList);
    }



    /*6.1.根据客户的openId获取对应的收藏good对象列表*/
    @RequestMapping("/getCollectionGoodInfoByOpenId")
    public  List<Good> getCollectionGoodInfoByOpenId(@RequestParam("openId")String openId){
        /*根据openId获取cId*/
        String cId = getCIdByOpenId(openId);
        /*根据cId获取对应的收藏列表*/
        return getCollectionGoodInfoByCId(cId);
    }


    /*7.根据客户的cId获取对应的购物车cart信息列表*/
    @RequestMapping("/getCartInfoByCId")
    public List<Cart> getCartInfoByCId(@RequestParam("cId") String cId){
        LambdaQueryWrapper<Cart> CartQueryWrapper = new LambdaQueryWrapper<>();
        CartQueryWrapper.eq(StringUtils.isNotBlank(cId),Cart::getCid,cId);
        return CartService.list(CartQueryWrapper);
    }

    /*7.1.根据客户的openId获取对应的购物车cart信息列表*/
    @RequestMapping("/getCartInfoByOpenId")
    public List<Cart> getCartInfoByOpenId(@RequestParam("openId") String openId){
        /*根据openId获取cId*/
        String cId = getCIdByOpenId(openId);
        /*根据cId获取cart信息列表*/
        return getCartInfoByCId(cId);
    }

    /*8.根据客户的cId获取对应的购物车中商品的gId列表list*/
    @RequestMapping("/getCartGIdListByCId")
    public List<String> getCartGIdListByCId(@RequestParam("cId") String cId){
        List<String> GIdList = new ArrayList<>();
        /*根据cId获取cart信息列表*/
        for (Cart cart : getCartInfoByCId(cId)) {
            GIdList.add(cart.getGid());
        }
        return GIdList;
    }

    /*8.1根据客户的openId获取对应的购物车中商品的gId列表list*/
    @RequestMapping("/getCartGIdListByOpenId")
    public List<String> getCartGIdListByOpenId(@RequestParam("openId") String openId){
        /*根据openId获取对应的cId*/
        String cId = getCIdByOpenId(openId);
        /*根据客户的cId获取对应的购物车中商品的gId列表list*/
        return getCartGIdListByCId(cId);
    }


    /*9.根据客户的cId获取对应的购物车商品信息列表list*/
    @RequestMapping("/getCartGoodInfoListByCId")
    public List<cartWithGoodInfo> getCartGoodInfoListByCId(@RequestParam("cId")String cId){

        List<Cart> cartsInfo = getCartInfoByCId(cId);
        /*返回的列表*/
        List<cartWithGoodInfo> cartWithGoodList = new ArrayList<>();
        for (Cart cart : cartsInfo) {
            String gid = cart.getGid();
            Good good = goodController.getGoodInfoById(gid);
            cartWithGoodInfo cartWithGood = new cartWithGoodInfo(cart,good);
            cartWithGoodList.add(cartWithGood);
        }
        return cartWithGoodList;
    }

    /*9.1.根据客户的openId获取对应的购物车商品信息列表list*/
    @RequestMapping("/getCartGoodInfoListByOpenId")
    public List<cartWithGoodInfo> getCartGoodInfoListByOpenId(@RequestParam("openId")String openId){
        /*根据openId获取cId*/
        String cId = getCIdByOpenId(openId);
        return getCartGoodInfoListByCId(cId);
    }


    /*10.根据客户的cId获取对应的订单信息*/
    @RequestMapping("/getOrderInfoByCId")
    public List<Gorder> getOrderInfoByCId(@RequestParam("cId") String cId){
        LambdaQueryWrapper<Gorder> OrderQueryWrapper = new LambdaQueryWrapper<>();
        OrderQueryWrapper.eq(StringUtils.isNotBlank(cId),Gorder::getCid,cId);
        return GorderService.list(OrderQueryWrapper);
    }


    /*10.1.根据客户的openId获取对应的订单信息*/
    @RequestMapping("/getOrderInfoByOpenId")
    public List<Gorder> getOrderInfoByOpenId(@RequestParam("openId")String openId){
        /*根据openId获取cId*/
        String cId = getCIdByOpenId(openId);
        return getOrderInfoByCId(cId);
    }


    /*10.2 根据订单id获取订单信息*/
    @RequestMapping("/getGorderInfoById")
    public Gorder getGorderInfoById(@RequestParam("oId") String oId){
        LambdaQueryWrapper<Gorder> GorderQueryWrapper = new LambdaQueryWrapper<>();
        GorderQueryWrapper.eq(StringUtils.isNotBlank(oId),Gorder::getOrderid,oId);
        return GorderService.getOne(GorderQueryWrapper);
    }


    /*11.根据客户的cId获取拥有的优惠卷信息*/
    @RequestMapping("/getGoodpreListByCId")
    public List<Goodpre> getGoodpreListByCId(@RequestParam("cId")String cId){
        LambdaQueryWrapper<Goodpre> GorderQueryWrapper = new LambdaQueryWrapper<>();
        GorderQueryWrapper.eq(StringUtils.isNotBlank(cId),Goodpre::getCid,cId);
        return GoodpreService.list(GorderQueryWrapper);
    }


    /*11.1根据客户的openId获取拥有的优惠卷信息*/
    @RequestMapping("/getGoodpreListByOpenId")
    public List<Goodpre> getGoodpreListByOpenId(@RequestParam("openId")String openId){
        /*根据openId获取cId*/
        String cId = getCIdByOpenId(openId);
        return getGoodpreListByCId(cId);
    }

    /*11.2根据优惠卷id查询优惠卷信息*/
    @RequestMapping("/getGoodPreInfoById")
    public Goodpre getGoodPreInfoById(@RequestParam("gPId") String gPId){
        return GoodpreService.getById(gPId);
    }

    /*12 根据订单号查询对应评论信息列表*/
    @RequestMapping("/getEvaluationListByOId")
    public List<Evaluation> getEvaluationListByOId(@RequestParam("oId")String oId){
        LambdaQueryWrapper<Evaluation> EvaluationQueryWrapper = new LambdaQueryWrapper<>();
        EvaluationQueryWrapper.eq(StringUtils.isNotBlank(oId),Evaluation::getOrderid,oId);
        return EvaluationService.list(EvaluationQueryWrapper);
    }

    /*12.1根据gid查询对应所有的订单评论信息*/
    @RequestMapping("/getEvaluationListByGId")
    public List<Evaluation> getEvaluationListByGId(@RequestParam("gId")String gId){
        /*首先根据gId查询出所有对应的order对象*/
        LambdaQueryWrapper<Gorder> GorderQueryWrapper = new LambdaQueryWrapper<>();
        GorderQueryWrapper.eq(StringUtils.isNotBlank(gId),Gorder::getGid,gId);
        List<Gorder> gorderList = GorderService.list(GorderQueryWrapper);
        /*收集订单id,并根据oId组装list*/
        List<Evaluation> EvaluationList = new ArrayList<>();
        for (Gorder gorder : gorderList) {
            /*获取oId*/
            String orderid = gorder.getOrderid();
            /*根据oId获取评论*/
            List<Evaluation> evaluationList = getEvaluationListByOId(orderid);
            EvaluationList.addAll(evaluationList);
        }
        return EvaluationList;
    }

    /*13根据评论id拆线呢对应评论信息*/
    @RequestMapping("/getEvaluationById")
    public Evaluation getEvaluationById(@RequestParam("eId")String eId){
        LambdaQueryWrapper<Evaluation> EvaluationQueryWrapper = new LambdaQueryWrapper<>();
        EvaluationQueryWrapper.eq(StringUtils.isNotBlank(eId),Evaluation::getOrderid,eId);
        return EvaluationService.getById(eId);
    }

    /*14.根据评论id查询对应图片列表*/
    @RequestMapping("/getEvaluationImgById")
    public List<Evaluateimg> getEvaluationImgById(@RequestParam("eId")String eId){
        LambdaQueryWrapper<Evaluateimg> EvaluateimgQueryWrapper = new LambdaQueryWrapper<>();
        EvaluateimgQueryWrapper.eq(StringUtils.isNotBlank(eId),Evaluateimg::getEvaluationid,eId);
        return EvaluateimgService.list(EvaluateimgQueryWrapper);
    }


    /*15.根据订单id查询退货信息*/
    @RequestMapping("/getReturnGoodByOId")
    public Returngood getReturnGoodByOId(@RequestParam("oId")String oId){
        /*根据oId查询对应的退货信息*/
        LambdaQueryWrapper<Returngood> ReturngoodQueryWrapper = new LambdaQueryWrapper<>();
        ReturngoodQueryWrapper.eq(StringUtils.isNotBlank(oId),Returngood::getOrderid,oId);
        return ReturngoodService.getOne(ReturngoodQueryWrapper);
    }


    /*16.根据退货id获取退货信息*/
    @RequestMapping("/getReturnGoodByRId")
    public Returngood getReturnGoodByRId(@RequestParam("rId")String rId){
        LambdaQueryWrapper<Returngood> ReturngoodQueryWrapper = new LambdaQueryWrapper<>();
        ReturngoodQueryWrapper.eq(StringUtils.isNotBlank(rId),Returngood::getReturnid,rId);
        return ReturngoodService.getOne(ReturngoodQueryWrapper);
    }

    /*没有加入*/

    /*17.根据状态获取对应状态的订单信息(包含商品信息)PC*/
    @RequestMapping("/getOWGBystatuePC")
    public List<GorderWithGoodInfo> getOWGBystatuePC(
            @RequestParam("cId") String cId,
            @RequestParam("orderStatue") String orderStatue
    ){
        LambdaQueryWrapper<Gorder> GorderQueryWrapper = new LambdaQueryWrapper<>();
        GorderQueryWrapper.eq(StringUtils.isNotBlank(cId),Gorder::getCid,cId).eq(true,Gorder::getOrderstatue,orderStatue);
        List<Gorder> gorderList = GorderService.list(GorderQueryWrapper);
        /*创建即将返回的列表*/
        List<GorderWithGoodInfo> GorderWithGoodInfoList = new ArrayList<>();
        for (Gorder gorder : gorderList) {
            /*获取gId*/
            String gid = gorder.getGid();
            Good good = goodController.getGoodInfoById(gid);
            /*创建gorderwithgood*/
            GorderWithGoodInfo GorderWithGoodInfo = new GorderWithGoodInfo(
                    gorder,
                    good
            );
            /*加入列表中*/
            GorderWithGoodInfoList.add(GorderWithGoodInfo);
        }
        return GorderWithGoodInfoList;
    }

    /*没有添加*/
    /*18.根据状态嘛获取对应状态的订单信息(包含商品信息)WX*/
    @RequestMapping("/getOWGBystatueWX")
    public List<GorderWithGoodInfo> getOWGBystatueWX(
            @RequestParam("openId") String openId,
            @RequestParam("orderStatue") String orderStatue
    ){
        /*根据openId获取cId*/
        String cId = getCIdByOpenId(openId);
        return getOWGBystatuePC(cId, orderStatue);

    }

    /*19.通过评价信息获得对应的新对象，包含客户信息，评论信息，图片信息*/
    public evaluationWithCustomerWithImg getEGIByEvaluation(
            Evaluation evaluation
    ){
        /*定义返回数据*/
        evaluationWithCustomerWithImg evaluationWithCustomerWithImg = new evaluationWithCustomerWithImg();
        /*1.设置评论信息*/
        evaluationWithCustomerWithImg.setEvaluation(evaluation);
        /*获取客户信息*/
        String orderid = evaluation.getOrderid();
        /*根据订单id获取订单信息*/
        Gorder gorder = GorderService.getById(orderid);
        /*获取客户id*/
        String cid = gorder.getCid();
        /*获取客户id*/
        Customer customer = CustomerService.getById(cid);
        /*2.设置客户信息*/
        evaluationWithCustomerWithImg.setCustomer(customer);
        /*查询图片信息*/
        String evaluationid = evaluation.getEvaluationid();
        LambdaQueryWrapper<Evaluateimg> EvaluateimgQueryWrapper = new LambdaQueryWrapper<>();
        EvaluateimgQueryWrapper.eq(StringUtils.isNotBlank(evaluationid),Evaluateimg::getEvaluationid,evaluationid);
        List<Evaluateimg> Evaluateimglist = EvaluateimgService.list(EvaluateimgQueryWrapper);
        evaluationWithCustomerWithImg.setEvaluateimgList(Evaluateimglist);
        return evaluationWithCustomerWithImg;

    }

    /*19.根据gId获取所有评论信息，包括客户信息，图片信息*/
    @RequestMapping("/getEGIByGId")
    public List<evaluationWithCustomerWithImg> getEGIByGId(
            @RequestParam("gId")String gId
    ){
        /*返回列表*/
        List<evaluationWithCustomerWithImg> evaluationWithCustomerWithImgList = new ArrayList<>();
        List<Evaluation> evaluationList = getEvaluationListByGId(gId);
        for (Evaluation evaluation : evaluationList) {
            evaluationWithCustomerWithImg evaluationWithCustomerWithImg = getEGIByEvaluation(evaluation);
            evaluationWithCustomerWithImgList.add(evaluationWithCustomerWithImg);
        }
        System.out.println(evaluationList);
        return evaluationWithCustomerWithImgList;
    }

    /*20.根据订单id获取所有评论信息，包括客户信息，图片信息*/
    @RequestMapping("/getEGIByOId")
    public List<evaluationWithCustomerWithImg> getEGIByOId(
        @RequestParam("oId") String oId
    ){
        /*返回列表*/
        List<evaluationWithCustomerWithImg> evaluationWithCustomerWithImgList = new ArrayList<>();
        /*根据订单获取评论信息*/
        List<Evaluation> evaluationList = getEvaluationListByOId(oId);
        for (Evaluation evaluation : evaluationList) {
            evaluationWithCustomerWithImg evaluationWithCustomerWithImg = getEGIByEvaluation(evaluation);
            evaluationWithCustomerWithImgList.add(evaluationWithCustomerWithImg);
        }
        return evaluationWithCustomerWithImgList;
    }


    /*21.客户cid获取所有评论信息，包括评论图片*/
    @RequestMapping("/getEGIByCId")
    public List<evaluationWithCustomerWithImg> getEGIByCId(
        @RequestParam("cId")String cId
    ){
        /*返回列表*/
        List<evaluationWithCustomerWithImg> evaluationWithCustomerWithImgList = new ArrayList<>();
        /*根据cId获取所有订单信息*/
        LambdaQueryWrapper<Gorder> GorderQueryWrapper = new LambdaQueryWrapper<>();
        GorderQueryWrapper
                .eq(StringUtils.isNotBlank(cId),Gorder::getCid,cId);
        List<Gorder> GorderList = GorderService.list(GorderQueryWrapper);
        /*根据订单信息获取所有评论*/
        for (Gorder gorder : GorderList) {
            String orderid = gorder.getOrderid();
            List<evaluationWithCustomerWithImg> partList = getEGIByOId(orderid);
            evaluationWithCustomerWithImgList.addAll(partList);
        }
        return evaluationWithCustomerWithImgList;
    }

    /*22.客户openId获取所有评论信息，包括评论图片*/
    @RequestMapping("/getEGIByOpenId")
    public List<evaluationWithCustomerWithImg> getEGIByOpenId(
            @RequestParam("openId")String openId
    ){
        String cId = getCIdByOpenId(openId);
        return getEGIByCId(cId);
    }



}
